package com.framework.modules.base.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.common.annotation.Login;
import com.framework.modules.base.entity.GiftEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.framework.modules.base.service.GiftService;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 礼物 
 *
 * @author MIT
 * @email litmiss@gmail.com
 * @date 2020-10-31 15:50:18
 */
@RestController
@RequestMapping("/gift")
@Api(tags = "礼物")
public class GiftController {
    @Autowired
    private GiftService giftService;

    /**
     *礼物列表
     * @param page
     * @param category 默认值为all 所有的礼物
     * @return
     * 返回参数
     * code 编码
     * type 类型
     * name 礼物名称
     * groupName 礼物分组
     * picUrl 礼物图片
     * price 礼物价格
     * descript 描述
     * display_order 排序
     * date_limit 有效期
     */
    @Login
    @RequestMapping("/list")
    @ApiOperation("礼物列表")
    public R list(@RequestParam(defaultValue = "0")Integer page,@RequestParam(defaultValue = "all") String category){
        Map<String, Object> params=new HashMap<>();
        params.put("category",category);
        params.put("page",page.toString());
        params.put("limit","1000");
        PageUtils pageUtils = giftService.queryPage(params);
        return R.ok(pageUtils);
    }


    /**
     * 获取礼物资源
     * @param groupName all　＝　全部礼物 , live = 直播室场景 , chat ＝　私聊场景
     * @return
     */
    @CrossOrigin
    @GetMapping("/query")
    @ApiOperation("获取礼物资源")
    public R query(@RequestParam(value = "group_name",defaultValue = "all") String groupName) {

        String[] allowGroups = new String[]{
                "all","live", "chat"
        };

        if (Arrays.asList(allowGroups).contains(groupName)) {
            EntityWrapper<GiftEntity> filter = new EntityWrapper<GiftEntity>();
            if(!"all".equals(groupName)){
                filter.eq("group_name", groupName);
            }
            List<GiftEntity> datalist = giftService.selectList(filter);
            return R.ok().put("data", datalist);
        }
        return R.error("礼物资源获取失败");
    }
}
